package com.xinghai.web.controller.system;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.xinghai.common.exception.ServiceException;
import java.io.IOException;

import com.xinghai.common.utils.SecurityUtils;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.common.utils.uuid.IdUtils;
import com.xinghai.system.domain.QdStore;
import com.xinghai.system.service.IQdStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.xinghai.common.constant.GenConstants;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.system.domain.QdUser;
import com.xinghai.system.service.IQdUserService;
import com.xinghai.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;
import com.xinghai.common.core.page.TableDataInfo;

/**
 * 用户Controller
 *
 * @author xinghai
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/system/qdUser")
public class QdUserController extends BaseController
{
    @Autowired
    private IQdUserService qdUserService;
    @Autowired
    private IQdStoreService storeService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(QdUser qdUser)
    {
        startPage();
        List<QdUser> list = qdUserService.list(qdUser);
        return getDataTable(list);
    }

    @GetMapping("/getList")
    public AjaxResult getList(QdUser qdUser)
    {
        qdUser.setStatus("0");
        List<QdUser> list = qdUserService.list(qdUser);
        return success(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QdUser qdUser)
    {
        List<QdUser> list = qdUserService.list(qdUser);
        ExcelUtil<QdUser> util = new ExcelUtil<QdUser>(QdUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:query')")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(qdUserService.getById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody QdUser qdUser)
    {
        qdUser.setAvatar("https://img1.baidu.com/it/u=3200425930,2413475553&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800");
        qdUser.setPassword(SecurityUtils.encryptPassword("123456"));
        qdUser.setInviteCode(IdUtils.randomNanoIdId());
        if (StringUtils.isBlank(qdUser.getUserPhone())||qdUser.getUserPhone().length()!=11){
            return error("请输入正确的手机号");
        }
        if(qdUserService.getByUserPhone(qdUser.getUserPhone())!=null){
            return error("该手机号已存在");
        }
        if (qdUser.getSuperId() != null){
            QdUser user = qdUserService.getById(qdUser.getSuperId());
            qdUser.setSuperUserPhone(user.getUserPhone());
            qdUser.setSuperUserName(user.getNickName());
            qdUser.setSuperAvatar(user.getAvatar());
            if (user.getAscriptionStore() != null){
                QdStore store = storeService.getById(user.getAscriptionStore());
                qdUser.setAscriptionStore(store.getId());
                qdUser.setStoreName(store.getStoreName());
                qdUser.setStoreLeader(store.getStoreLeader());
                qdUser.setStoreLeaderPhone(store.getStoreLeaderPhone());
                qdUser.setStoreLeaderId(store.getStoreLeaderId());
                qdUser.setStoreLeaderImg(store.getStoreLeaderImg());
            }
        }
        qdUser.setTeamNum(1);
        qdUser.setGiftValue(new BigDecimal("3.5"));
        qdUser.setGroupTwoPower("1");
        qdUser.setGroupOnePower("1");
        qdUser.setGroupThreePower("1");
        return toAjax(qdUserService.saveInfo(qdUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody QdUser qdUser)
    {
        return toAjax(qdUserService.updateById(qdUser));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id)
    {
        return toAjax(qdUserService.delete(id,GenConstants.DELETE_FLAG_TRUE));
    }


    /**
     * 批量删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids)
    {
        return toAjax(qdUserService.deleteBatch(ids,GenConstants.DELETE_FLAG_TRUE));
    }

    /**
     * 导入用户模板
     */
    @PreAuthorize("@ss.hasPermi('system:qdUser:import')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<QdUser> util = new ExcelUtil<QdUser>(QdUser.class);
        util.importTemplateExcel(response, "用户数据模板");
    }


    /**
     * 导入用户
     */
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:qdUser:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<QdUser> util = new ExcelUtil<QdUser>(QdUser.class);
        List<QdUser> qdUserList = null;
        try {
            qdUserList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(qdUserService.importData(qdUserList));
    }
}
